import javax.swing.*;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.TimeZone;
import java.util.Date;
import java.io.*;

public class AddEvent extends JFrame implements ActionListener{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JComboBox boxDay;
	private JComboBox boxMonth;
	private JComboBox boxYear;
	private JComboBox boxStartHour;
	private JComboBox boxStartMinute;
	private JComboBox boxEndHour;
	private JComboBox boxEndMinute;
	private JTextField title;  
	private JTextField location;
	private JTextField comment;
	private JTextField latitude;
	private JTextField longitude;
	private JTextField resources;
	private JRadioButton radioPublic;
	private JRadioButton radioPrivate;
	private JRadioButton radioConfidential;
	private JButton createEvent;
	private JButton reset;
	private JButton close;
	// strings to send to .ics generator
	private String date;
	private String startTime;
	private String endTime;
	private String classType;
	private String months[];
	private String days[];
	private String years[];
	private String hours[];
	private String minutes[];
	private String data;
	private int inc;
	
	

	public AddEvent (int width, int height) {
		super("Add New Event"); // title of the window
		setLayout(new GridLayout(8,1));  // general layout is one panel per row 
		setBounds(0,0,width,height);
		months = createCombo(1,12);  // string array to hold the numbers representing the months
		days = createCombo(1,31);   // string array to hold the numbers representing the days
		years = createCombo(2011,20);  // string array to hold the numbers representing the years
		hours = createCombo(0,24); // string array to hold the numbers representing the hours
		minutes = createCombo(0,60); // string array to hold the numbers representing the minutes

		/*
		 * Panels that will hold the different sections of the GUI
		 */
		JPanel panDateTime = new JPanel(new FlowLayout(FlowLayout.LEFT));// date and start/end times of the event
		JPanel panDate = new JPanel();
		JPanel panTime = new JPanel();
		JPanel panTitle = new JPanel(new FlowLayout(FlowLayout.LEFT));// the event title
		JPanel panLocation = new JPanel(new FlowLayout(FlowLayout.LEFT));// the location
		JPanel panGeo = new JPanel(new FlowLayout(FlowLayout.LEFT));// the geographic position
		JPanel panClass = new JPanel(new FlowLayout(FlowLayout.LEFT));// the class type
		JPanel panResources = new JPanel(new FlowLayout(FlowLayout.LEFT));// the resources 
		JPanel panComment = new JPanel(new FlowLayout(FlowLayout.LEFT));// the comment 
		JPanel panButtons = new JPanel();// the buttons
		
		/*
		 * Labels for the panels
		 */
		JLabel labelDate = new JLabel("Date:");
		JLabel labelStart = new JLabel("Start Time:");
		JLabel labelEnd = new JLabel("Finish Time:");
		JLabel labelTitle = new JLabel("Event Title:");
		JLabel labelLocation = new JLabel("Location:");
		JLabel labelGeo = new JLabel("Geographic Position");
		JLabel labelLat = new JLabel("Latitude:");
		JLabel labelLon = new JLabel("Longitude:");
		JLabel labelClass = new JLabel("Class:");
		JLabel labelResources = new JLabel("Resources:");
		JLabel labelComment = new JLabel("Comment:");
		
		// comboboxes to hold the dates and times
		boxMonth = new JComboBox(months);
		boxDay = new JComboBox(days);
		boxYear = new JComboBox(years);
		boxStartHour = new JComboBox(hours);
		boxStartMinute = new JComboBox(minutes);
		boxEndHour = new JComboBox(hours);
		boxEndMinute = new JComboBox(minutes);
		
		title = new JTextField("Untitled", 40); // text field for the title of the event
		location = new JTextField(40); // text fields for the location of the event
		comment = new JTextField(40); // text field for the comments
		latitude = new JTextField(10);
		longitude = new JTextField(10);
		resources = new JTextField(40);
		
		// radio buttons for the class type of the event
		radioPublic = new JRadioButton();
		radioPublic.setSelected(true);
		radioPublic.setText("Public");
		radioPrivate = new JRadioButton();
		radioPrivate.setText("Private");
		radioConfidential = new JRadioButton();
		radioConfidential.setText("Confidential");
		// group the buttons together so that only one can be selected at a time
		ButtonGroup classes = new ButtonGroup();
		classes.add(radioPublic);
		classes.add(radioPrivate);
		classes.add(radioConfidential);
		
		// buttons at the bottom of the GUI that will reset or send the info to the .ics generator
		createEvent = new JButton("Create Event");
		reset = new JButton("Reset");
		close = new JButton("Close");
		createEvent.addActionListener(this);
		reset.addActionListener(this);
		close.addActionListener(this);
		
		// build the date panel
		panDate.add(labelDate);
		panDate.add(boxMonth);
		panDate.add(boxDay);
		panDate.add(boxYear);
		// build the time panel
		panTime.add(labelStart);
		panTime.add(boxStartHour);
		panTime.add(boxStartMinute);
		panTime.add(labelEnd);
		panTime.add(boxEndHour);
		panTime.add(boxEndMinute);
		// combine the date and time panels into another panel
		panDateTime.add(panDate);
		panDateTime.add(panTime);
		
		// build the title panel
		panTitle.add(labelTitle); 
		panTitle.add(title);
		
		// build the location panel
		panLocation.add(labelLocation);
		panLocation.add(location);
		
		// build the geographic position panel
		panGeo.add(labelGeo);
		panGeo.add(labelLat);
		panGeo.add(latitude);
		panGeo.add(labelLon);
		panGeo.add(longitude);
		
		// build the class panel
		panClass.add(labelClass);
		panClass.add(radioPublic);
		panClass.add(radioPrivate);
		panClass.add(radioConfidential);
		
		// build the resources panel
		panResources.add(labelResources);
		panResources.add(resources);
		
		// build the comment panel
		panComment.add(labelComment);
		panComment.add(comment);
		
		// build the buttons panel
		panButtons.add(createEvent);
		panButtons.add(reset);
		panButtons.add(close);
		
		// build the main sections of the GUI
		add(panDateTime);
		add(panTitle);
		add(panLocation);
		add(panGeo);
		add(panClass);
		add(panResources);
		add(panComment);
		add(panButtons);
		setVisible(true);  // show the GUI as visible to the user
	}

	//this is a code change
	
	/**
	 * 
	 * @param j starts the sequence of integers to be stored in the array
	 * @param end size of the array
	 * @return a string array consisting of a sequence of integers
	 */
	private String[] createCombo(int j, int end) {
		// temporary array to hold the sequence
		String newCombo[] = new String[end];
		// loop to populate the array for months
		for (int i = 0; i < end; i++) {
			data = "";
			if (j < 10) {
				data = "0";
			}
			newCombo[i] = data + j;
			j++;
		}
		return newCombo;  // returns the array
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == createEvent)
			try {
				createICS();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		else if (e.getSource() == reset)
			resetGUI();
		else if (e.getSource() == close)
			System.exit(0);  // close program without errors
			
	}
	
	/**
	 * resets the text values in the GUI
	 */
	private void resetGUI() {
		location.setText(""); 
		title.setText("Untitled");
		comment.setText("");
	}

	public void createICS() throws IOException {
		// Generates the .ics file	
		getValuesReady();
		  File f;
		  f=new File("Event" + date.toString() + ".ics");
		  if(!f.exists()){
			  f.createNewFile();
			  System.out.println("New file \"Event" + date.toString() + ".ics\" has been created to the current directory");
		  }

		  Writer output = null;

		  output = new BufferedWriter(new FileWriter(f));
		  output.write("BEGIN:VCALENDAR" + "\r\n" + "PRODID:-//Google Inc//Google Calendar 70.9054//EN" + "\r\n");
		  output.write("VERSION:2.0" + "\r\n");
		  output.write("METHOD:PUBLISH" + "\r\n");
		  output.write("BEGIN:VEVENT" + "\r\n");
		  output.write("CLASS:" + classType + "\r\n");
		  output.write("COMMENT:" + comment.getText() + "\r\n");
		  output.write("LOCATION:" + location.getText() + "\r\n");
		  output.write("GEO:" + latitude.getText() + ";" + longitude.getText() + "\r\n");
		  output.write("RESOURCES:" + resources.getText() + "\r\n");
		  output.write("DTSTART:" + date + "T" + startTime + "00" + "\r\n");
		  output.write("DTEND:" + date + "T" + endTime + "00" + "\r\n");
		  output.write("TZID:" + TimeZone.getDefault().getID() + "\r\n");
		  output.write("SUMMARY:" + title.getText() + "\r\n");
		  output.write("END:VEVENT" + "\r\n");
		  output.write("END:VCALENDAR" + "\r\n");

          try
          {
                  if ( output != null)
                          output.close( );
          }
          catch ( IOException e)
          {
          }
		 //* Edit the following lines to generate the .ics file
		System.out.println("The date is: " + date); //debug
		System.out.println("The start time is: " + startTime);//debug
		System.out.println("The finish time is: " + endTime); //debug
		System.out.println("The class is: " + classType); //debug
		System.out.println("The event title is: " + title.getText()); //debug
		System.out.println("The location is: " + location.getText()); //debug
		System.out.println("The latitude is: " + latitude.getText() + " and the longitude is: " + longitude.getText());// debug
		System.out.println("The comment is: " + comment.getText()); //debug
		System.out.println("The time zone is: " + TimeZone.getDefault().getID());
	}

	private void getValuesReady() {
		date = (String)boxYear.getSelectedItem() + (String)boxMonth.getSelectedItem() + (String)boxDay.getSelectedItem();// format the date field
		startTime = (String)boxStartHour.getSelectedItem() + (String)boxStartMinute.getSelectedItem();  // format the start time
		endTime = (String)boxEndHour.getSelectedItem() + (String)boxEndMinute.getSelectedItem(); // format the end time
		if (radioPublic.isSelected())  // get the selected class type
			classType = "PUBLIC";
		else if (radioPrivate.isSelected()) 
			classType = "PRIVATE";
		else 
			classType = "CONFIDENTIAL";	
	}

	/*
	 * 	I added these to help test the jUnit stuff.  Sorry, I couldn't figure any other way out  --Tyler
	 * */
	public String getDate() {return date;}
	public String getStartTime() { return startTime; }
	public String getEndTime() { return endTime; }
	public String getClassType() { return classType; }
	public String getResources() {return resources.getText(); }
	public String getTimeZone() { return TimeZone.getDefault().getID(); }
	public AddEvent() {
		date = "TEST";
		startTime = "TEST";
		endTime = "TEST";
		classType = "TEST";
		resources = null;
	}
   
   
	/**
	 * @param args command line arguments
	 */
	public static void main(String[] args) {
		// Creates a new frame to represent the menu
		JFrame frame = new AddEvent(900,400);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

}
